/**
* @author:kellen
* @contact:xiekellen@hotmail.com
* @company:achivo-gz
* @created on:
* @modified on:
*/

package com.sasis.dao.ibatis;

import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.FlightState;
import com.sasis.dao.FlightStateDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class FlightStateDaoiBatis extends BaseDaoiBATIS implements FlightStateDao {

    /**
     * @see com.sasis.dao.FlightStateDao#getFlightStates(com.sasis.model.FlightState)
     */
    public List getFlightStates(final FlightState flightState) {
          return getSqlMapClientTemplate().queryForList("getFlightStates", flightState);
    }

    /**
     * @see com.sasis.dao.FlightStateDao#getFlightState(Integer id)
     */
    public FlightState getFlightState(Integer id) {
        FlightState flightState = (FlightState) getSqlMapClientTemplate().queryForObject("getFlightState", id);

        if (flightState == null) {
            throw new ObjectRetrievalFailureException(FlightState.class, id);
        }

        return flightState;
    }

    /**
     * @see com.sasis.dao.FlightStateDao#saveFlightState(FlightState flightState)
     */    
    public void saveFlightState(final FlightState flightState) {
        Integer id = flightState.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addFlightState", flightState);
        } else {
            getSqlMapClientTemplate().update("updateFlightState", flightState);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(FlightState.class, id);
        }
    }

    /**
     * @see com.sasis.dao.FlightStateDao#removeFlightState(Integer id)
     */
    public void removeFlightState(FlightState flightState) {
        getSqlMapClientTemplate().update("deleteFlightState",  flightState);
    }
}
